package com.mic.yzzn.plc.common.instructions.response;

import org.apache.commons.lang.StringUtils;

import com.mic.yzzn.plc.common.instructions.basic.PLCResponseInstruction;

public class PLCMultipleWriteResponse extends PLCResponseInstruction
{
	/**
	 * 初始化方法
	 */
	public PLCMultipleWriteResponse()
	{
		new PLCResponseInstruction();
		
        instructionTypeCode = PLCResponseInstruction.PLCInstructionTypeResponseCode;
		responseCode = PLCResponseInstruction.WRITE_RESPONSE_CODE;
	}
	
	/**
	 * 用字符串初始化
	 */
    @Override
    public String initWithString(String instructionString) throws IllegalArgumentException
    {
    	String tempResult = super.initWithString(instructionString);
    	
//    	System.out.println(tempResult);
    	// 检测是否已经出错
    	if (tempResult.length() == 0)
		{
			return tempResult;
		}
    	
    	// 判断响应码与当前指令的响应码是否符合
    	int requestCodeLength = responseCode.length();
    	if (!(StringUtils.substring(tempResult, 0, requestCodeLength).equals(responseCode)))
		{
			throw new IllegalArgumentException("PLCSingleContactorReadInstruction's requestCode must be \"" + responseCode +"\"");
		}
    	
    	// 截取响应码
    	tempResult = StringUtils.substringAfter(tempResult, responseCode);
//    	System.out.println(tempResult);
    	
    	return tempResult;
    }
}
